package com.eastfair.questionnaire.controller;

import com.eastfair.core.base.R;
import com.eastfair.core.base.entity.SuperEntity;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.projectcore.api.DictionaryFeign;
import com.eastfair.questionnaire.dto.DisplayLocationQuestionDTO;
import com.eastfair.questionnaire.dto.DisplayLocationQuestionnaireDTO;
import com.eastfair.questionnaire.dto.DisplayLocationQuestionnaireQuery;
import com.eastfair.questionnaire.service.DisplayLocationService;
import com.eastfair.questionnaire.vo.DisplayLocationQuestionVO;
import com.eastfair.questionnaire.vo.DisplayLocationQuestionnaireVO;
import com.eastfair.questionnaire.vo.DynamicQuestionnaireVO;
import com.eastfair.questionnaire.vo.QuestionnaireVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * <p>
 * 问卷在不同位置配置
 * </p>
 *
 * @author yuwx
 * @date 2021-08-18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/displayLocation")
@Api(value = "displayLocation", tags = "问卷在不同位置配置")
public class DisplayLocationController {

    private DisplayLocationService displayLocationService;
    private EchoService echoService;
    private DictionaryFeign dictionaryFeign;
    @Autowired
    public DisplayLocationController(DisplayLocationService displayLocationService, EchoService echoService, DictionaryFeign dictionaryFeign) {
        this.displayLocationService = displayLocationService;
        this.echoService = echoService;
        this.dictionaryFeign = dictionaryFeign;
    }

    @ApiOperation(value = "添加位置", notes = "添加位置")
    @RequestMapping(value = "/addDisplayLocation", method = {RequestMethod.POST, RequestMethod.PUT})
    public R<Boolean> addDisplayLocationQuestionnaire(@RequestBody @Validated(SuperEntity.Save.class) DisplayLocationQuestionnaireDTO displayLocationQuestionnaireDTO) {
        displayLocationService.addDisplayLocationQuestionnaire(displayLocationQuestionnaireDTO.getDisplayLocation(), displayLocationQuestionnaireDTO.getQuestionnaireId());
        return R.success();
    }
    @ApiOperation(value = "更新位置问卷", notes = "更新位置问卷")
    @RequestMapping(value = "/updateDisplayLocation", method = {RequestMethod.POST, RequestMethod.PUT})
    public R<Boolean> updateDisplayLocationQuestionnaire(@RequestBody @Validated(SuperEntity.Update.class) DisplayLocationQuestionnaireDTO displayLocationQuestionnaireDTO) {
        displayLocationService.updateDisplayLocationQuestionnaire(displayLocationQuestionnaireDTO.getId(), displayLocationQuestionnaireDTO.getQuestionnaireId());
        return R.success();
    }

    @ApiOperation(value = "位置列表", notes = "位置列表")
    @RequestMapping(value = "/listDisplayLocation", method = {RequestMethod.POST})
    public R<List<DisplayLocationQuestionnaireVO>> listDisplayLocationQuestionnaire() {
        Map<Serializable, Object> map = dictionaryFeign.findByIds(Stream.of("MANUAL").collect(Collectors.toSet()));
        List<DisplayLocationQuestionnaireDTO> displayLocationQuestionnaireDTOList =
                displayLocationService.listDisplayLocationQuestionnaire();
        List<DisplayLocationQuestionnaireVO> displayLocationQuestionnaireVOList =
                ConvertUtil.convertList(displayLocationQuestionnaireDTOList, DisplayLocationQuestionnaireVO.class);
        echoService.action(displayLocationQuestionnaireVOList);
        return R.success(displayLocationQuestionnaireVOList);

    }

    @ApiOperation(value = "更新位置下问题配置", notes = "更新位置下问题配置")
    @RequestMapping(value = "/updateDisplayLocationQuestion", method = {RequestMethod.POST, RequestMethod.PUT})
    public R<Boolean> updateDisplayLocationQuestion(@RequestBody @Validated(SuperEntity.Update.class) DisplayLocationQuestionDTO displayLocationQuestionDTO) {
        if (Objects.isNull(displayLocationQuestionDTO.getId())) {
            displayLocationService.addDisplayLocationQuestion(displayLocationQuestionDTO);
        } else {
            displayLocationService.updateDisplayLocationQuestion(displayLocationQuestionDTO);
        }
        return R.success();
    }

    @ApiOperation(value = "位置下问题配置列表", notes = "位置下问题配置列表")
    @RequestMapping(value = "/listDisplayLocationQuestion", method = {RequestMethod.POST})
    public R<List<DisplayLocationQuestionVO>> listDisplayLocationQuestion(@RequestBody  DisplayLocationQuestionDTO displayLocationQuestionDTO) {
        List<DisplayLocationQuestionDTO> displayLocationQuestionDTOList = displayLocationService.listWrapDisplayLocationQuestion(displayLocationQuestionDTO.getDisplayLocationQuestionnaireId());

        List<DisplayLocationQuestionVO> displayLocationQuestionVOList = ConvertUtil.convertList(displayLocationQuestionDTOList, DisplayLocationQuestionVO.class);
        echoService.action(displayLocationQuestionVOList);
        return R.success(displayLocationQuestionVOList);
    }

    @ApiOperation(value = "获取位置下问卷(包括答案)", notes = "获取位置下问卷(包括答案)")
    @RequestMapping(value = "/getQuestionnaireByDisplayLocation", method = {RequestMethod.POST})
    public R<DynamicQuestionnaireVO> getQuestionnaireByDisplayLocation(@RequestBody DisplayLocationQuestionnaireQuery displayLocationQuestionnaireQuery) {
        DynamicQuestionnaireVO dynamicQuestionnaireVO = displayLocationService.getQuestionnaireByDisplayLocation(
                displayLocationQuestionnaireQuery.getDisplayLocation(),
                displayLocationQuestionnaireQuery.getSubjectType(),
                displayLocationQuestionnaireQuery.getSubjectId());
        return R.success(dynamicQuestionnaireVO);
    }

    @ApiOperation(value = "获取位置下选项", notes = "获取位置下选项")
    @RequestMapping(value = "/getOptionByDisplayLocation", method = {RequestMethod.POST})
    public R<QuestionnaireVO> getOptionByDisplayLocation(@RequestBody  DisplayLocationQuestionnaireQuery displayLocationQuestionnaireQuery) {
        QuestionnaireVO questionnaireVO = displayLocationService.getOptionByDisplayLocation(
                displayLocationQuestionnaireQuery.getDisplayLocation(),
                displayLocationQuestionnaireQuery.getSubjectType(),
                displayLocationQuestionnaireQuery.getSubjectId());
        return R.success(questionnaireVO);
    }
}
